package com.lnkwe.controller;

import com.lnkwe.common.model.LoginUser;
import com.lnkwe.constant.SessionConstant;
import com.lnkwe.core.db.CommonResult;
import com.lnkwe.core.db.ResultStatus;
import com.lnkwe.sys.model.SysUser;
import com.lnkwe.sys.service.SysUserService;
import com.lnkwe.util.MD5Util;
import com.lnkwe.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by lingwo on 2016/9/19.
 */
@Controller
public class LoginController {
    @Autowired
    SysUserService sysUserService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public void login(LoginUser user, HttpServletRequest request, HttpServletResponse response) {
        //登陆页面，判断用户名与密码是否成功
        HttpSession session = request.getSession();
        String url = "";
        try {
            String userName = user.getUsername();
            String password = user.getPassword();
            CommonResult<SysUser> result = sysUserService.login(userName, MD5Util.toMd5(password));
            if (result.getStatus() == ResultStatus.SUCCESS) {
                SysUser sysUser = result.getResult();
                session.setAttribute(SessionConstant.SYSUSER, sysUser);
                url = (String) session.getAttribute(SessionConstant.REDIRECTURL);
                if (StringUtil.isNull(url)) {
                    url = "/index.html";
                }
                request.setAttribute("msg", "登陆成功!");
                response.sendRedirect(url);

            } else {

                session.setAttribute("msg", "用户名或密码错误");
                response.sendRedirect("/login");
            }


        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "请输入用户名与密码");
        }


    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public void register(SysUser user, HttpServletRequest request, HttpServletResponse response) {
        try {
            SysUser model = user;
            SysUser sysUser = sysUserService.userName(user.getUserName());
            if (sysUser != null) {
                request.setAttribute("msg", "该用户名已注册，请重新输入!");
                response.sendRedirect("/register");
            } else {
                if (StringUtil.isNotNull(user.getPassword())) {
                    model.setPassword(MD5Util.toMd5(user.getPassword()));
                }
                model.setCreateTime(new Date());
                CommonResult<SysUser> result = sysUserService.save(model);
                if (result.getStatus() == ResultStatus.SUCCESS) {
                    response.sendRedirect("/login");
                    request.setAttribute("msg", "注册成功!");
                } else {
                    request.setAttribute("msg", "注册失败!");
                    response.sendRedirect("/register");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "注册失败!");
        }
    }


    @RequestMapping(value = "/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) {
        //登陆页面，判断用户名与密码是否成功
        HttpSession session = request.getSession();
        try {
            session.removeAttribute(SessionConstant.SYSUSER);
            response.sendRedirect("/login");
        } catch (Exception e) {
            e.printStackTrace();
        }
//        return "/admin/user/login";
    }
}
